package CtrLayer;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.Locale;

import DBLayer.DBJournal;
import DBLayer.DbConnection1;
import DBLayer.IFDBJournal;

import DomainLayer.Journal;
import DomainLayer.NutritionRiskScheme;



public class JournalCtr {
	
	private SchemeCtr schemeCtr;
	private IFDBJournal dbJournal;
	
	public JournalCtr()
	{
		
		schemeCtr=new SchemeCtr();
		dbJournal= new DBJournal();
	}

	
	public void createJournal(String name,String sex, int age, String hospDate, String relatives, String contactCauses, String hypersensitivity, String medicalDiagnosis,
			   String theme, String module, 
			 
			  String nutRiskName,String bscaleName, String nursDocName,String labSchemeName,String tempSchemeName)
	{
		
	
		
		

		 
		 Journal journalObj= new Journal();
		 
  	   journalObj.setName(name);
  	   journalObj.setSex(sex);
  	   journalObj.setAge(age);
  	   journalObj.setHosp_date(hospDate);
  	   journalObj.setRelatives(relatives);
  	   journalObj.setContact_causes(contactCauses);
  	   journalObj.setHypersensitivity(hypersensitivity);
  	   journalObj.setMedical_diagnosis(medicalDiagnosis);

  	   journalObj.setTheme(theme);
  	   journalObj.setModule(module);
		 
		try{
			
  	   journalObj.setLabscheme(schemeCtr.getAllLabSchemeMeasurements(labSchemeName));
		journalObj.setNursDoc(schemeCtr.getNursingDocumentation(nursDocName));
		journalObj.setNutrisk((schemeCtr.findNutritionRiskScheme(nutRiskName)));
		journalObj.setTempScheme(schemeCtr.getAllTemperatureSchemeMeasurements(tempSchemeName));
		journalObj.setBscale(schemeCtr.findBradenScale(bscaleName));}
		catch(Exception ex)
		{
			System.out.println("exception caught");
		}
		DBJournal dbJournal = new DBJournal();
		DbConnection1.startTransaction();
		dbJournal.createJournal(journalObj);
		
		
		if(journalObj.getLabscheme()!=null)
	    dbJournal.connectJournaLabScheme(journalObj);
		
		if(journalObj.getTempScheme()!=null)
		dbJournal.connectJournalTemperatureScheme(journalObj);
		
		if(journalObj.getNursDoc()!=null){
			dbJournal.connectJournalDocumentation(journalObj);
		}
		
		if(!DbConnection1.commitTransaction())
		
		
			DbConnection1.rollbackTransaction();
		
	
	}
	
	public ArrayList<Journal> getAllJournals()
	{
		return null;
		
	}
	
	//returns journals with the specific theme and moduel
	public ArrayList<Journal> getJournals(String theme, String module)
	{
		
		return dbJournal.getJournals(theme, module, false);
	}
	
	/**
	 * Attach the given journal to the given bed
	 * @param journal the journal to be attached to the bed
	 * @param roomNo the room number where the bed is located 
	 * @param bedNo the bed number
	 */
	public void assignToBed(Journal journal,int roomNo, int bedNo,String date)
	{
		dbJournal.assignToBed(journal, roomNo, bedNo,date);
	}
	
	/**Returns all themes of a specific module, used for the accodrion menu 
	 * 
	 * @param moduleName
	 * @return List of themes
	 */
	
	public ArrayList<String> findAllThemes(String moduleName)
	{
		return dbJournal.findAllThemes(moduleName,true);
	}
	
	
	/**
	 * Returns all journals with the selected theme and moduel
	 * @param theme
	 * @param module
	 * @return list of journals
	 */
	public String[][] getAllJournals(String theme,String module)
	{
		ArrayList<Journal> journals= dbJournal.getJournals(theme, module, true);
		String [][] journalInfo = new String [journals.size()][3];
		
		for(int i=0;i<journals.size();i++)
		{
			Journal journal=journals.get(i);
			journalInfo[i][0]=journal.getName();
			journalInfo[i][1]=journal.getContact_causes();
			
			System.out.println(journal.getName()+ "Dan was here");
			
		
			
	
			
		}
	return journalInfo ;
	}
	
	/**
	 * Returns the general information about the patient with the specific journal(found using bed , room, date)
	 * @param bed_No
	 * @param room_No
	 * @param date
	 * @return information
	 */
	public String [][] getJournalInformationForSession(int bed_No,int room_No,String date)
	{
		
		String [][] information= new String [8][2];
		Journal journal=findJournalAccordingBed(bed_No, room_No, date);
		
		information[0][0]="Name";
		information[0][1]=journal.getName();
		
		information[1][0]="Age";
		information[1][1]=Integer.toString(journal.getAge());
		
		information[2][0]="Sex";
		information[2][1]=journal.getSex();
		
		information[3][0]="Contact Causes";
		information[3][1]=journal.getContact_causes();
		
		information[4][0]="Hypersensitivity";
		information[4][1]=journal.getHypersensitivity();
		
		information[5][0]="Medical Diagnosis";
		information[5][1]=journal.getMedical_diagnosis();
		
		information[6][0]="Hospitalization Date";
		information[6][1]=journal.getHosp_date();
		
		information[7][0]="Relatives";
		information[7][1]=journal.getRelatives();
		
		
		
	    
		
		
		
		return information;
		
		
	}


	public Journal findJournal(String journalName, String contactCauses) {
		return dbJournal.findJournal(journalName, contactCauses, false);
	}
	
	public Journal findJournalAccordingBed(int bed_No, int room_No, String date)
	{
		
		return dbJournal.findJournalAccordingBed(bed_No, room_No, date);
	}
	

	
}
